<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/12
 * Time: 8:48
 */

namespace app\portal\controller;
use app\portal\model\UserOrganizeModel;
use cmf\controller\HomeBaseController;
use app\portal\model\UserRankModel;
use app\portal\model\ActivityLogModel;
use app\portal\model\ActivityUploadModel;
use app\portal\model\DocumentCategoryModel;
use app\portal\model\DocumentModel;
use app\portal\model\UserModel;
use think\facade\Env;


use think\Db;


class VoluntController extends HomeBaseController
{
    //排行
    public function rank(){
        $id=input('id');
        $rank=new UserRankModel();
        if($id==1){
            $data=$rank->with('user')->order('duration desc')->select();
            $this->assign(['rank'=>$data,'org'=>'牵手','id'=>$id]);
        }elseif ($id==2){
            $data=$rank->with('user')->order('z_times desc')->select();
            $this->assign(['rank'=>$data,'org'=>'啄木鸟','id'=>$id]);
        }elseif($id==3){
            $rank=new UserRankModel();
            $data=$rank->with('user')->select();
            $list=[];
            foreach ($data as $k=>$v){
                @$list[$v['user']['community']]+=$v['all_points'];
            }
            unset($list[0]);
            unset($list['']);
            arsort($list);
            $array=[];
            foreach ($list as $k=>$v){
                $name=Db::name('community')->where('id',$k)->value('name');
                $a=[$name,$v];
                $array[]=$a;
            }
            $this->assign(['rank'=>$array,'org'=>'社区','id'=>$id]);
        }else{
            $rank=new UserRankModel();
            $data=$rank->with('user')->select();
            $list=[];
            foreach ($data as $k=>$v){
                @$list[$v['user']['group']]+=$v['all_points'];
            }
            unset($list[0]);
            unset($list['']);
            arsort($list);
            $array=[];
            foreach ($list as $k=>$v){
                $name=Db::name('organize_child')->where('id',$k)->value('name');
                $a=[$name,$v];
                $array[]=$a;
            }
            $this->assign(['rank'=>$array,'org'=>'团体','id'=>$id]);
        }
        $this->assign('org_id',$id);
        return $this->fetch(':voluntManagement');
    }
    //志愿者档案详情
    public function detail(){
        $user_id=input('user_id');
        $this->assign($this->get_userinfo($user_id));
        return $this->fetch(':voluntManageDetail');

    }
    //下载志愿者档案为word
    public function save_word(){
        $user_id=input('user_id');
        $data=$this->get_userinfo($user_id);
        $demopath=Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'html'.DIRECTORY_SEPARATOR.'word'.DIRECTORY_SEPARATOR.'index.docx';
       // $tmp=new \PhpOffice\PhpWord\TemplateProcessor('H:/rzjj/rzjj/public/html/word/index.docx');//打开模板
        $tmp=new \PhpOffice\PhpWord\TemplateProcessor($demopath);//打开模板
        $tmp->setValue('name',$data['info']['user_nickname']);
        $tmp->setValue('sex',$data['sex'][$data['info']['sex']]);
        $birthday=$data['info']['birthday']==0?'':date('Y-m-d',$data['info']['birthday']);
        $tmp->setValue('birthday',$birthday);
//        $tmp->setValue('native',$data['info']['more']['native']);
//        $tmp->setValue('educate',$data['info']['more']['educate']);
        $tmp->setValue('politic',$data['info']['more']['politic']);
//        $tmp->setValue('work',$data['info']['more']['work']);
        $tmp->setValue('id',$data['info']['id']);
//        $tmp->setValue('company',$data['info']['more']['company']);
        $tmp->setValue('community',$data['community']);
        $tmp->setValue('mobile',$data['info']['mobile']);
//        $tmp->setValue('qq',$data['info']['more']['qq']);
//        $tmp->setValue('weixin',$data['info']['more']['weixin']);
        $tmp->setValue('create_time',date('Y-m-d',$data['info']['create_time']));
//        $tmp->setValue('address',$data['info']['more']['address']);
//        $tmp->setValue('hobby',$data['info']['more']['hobby']);
        $tmp->setValue('total_time',s_to_h($data['total_duration']));
        $rows=count($data['qians_log']);//总行数
        $tmp->cloneRow('time',$rows);//复制行
        for($i=0;$i<$rows;$i++){
            $tmp->setValue("time#".($i+1),date('Y-m-d H:i',$data['qians_log'][$i]['start_time']).'----'.date('H:i',$data['qians_log'][$i]['end_time']));//替换变量
            $tmp->setValue("place#".($i+1),$data['qians_log'][$i]['place']);//替换变量
            $tmp->setValue("activity#".($i+1),$data['qians_log'][$i]['activity']['title']);
            $tmp->setValue("duration#".($i+1),s_to_h($data['qians_log'][$i]['duration']));
        }
        $rows=count($data['zhuom_log']);//总行数
        $tmp->cloneRow('ctime',$rows);//复制行
        for($i=0;$i<$rows;$i++){
            $tmp->setValue("ctime#".($i+1),date('Y-m-d H:i',$data['zhuom_log'][$i]['ctime']));//替换变量
            $tmp->setValue("places#".($i+1),$data['zhuom_log'][$i]['place']);//替换变量
            $tmp->setValue("content#".($i+1),$data['zhuom_log'][$i]['content']);
        }
        $url=$this->request->domain();
        $tmp->setImageValue('avatar',['path'=>Env::get('root_path').DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.$data['info']['avatar']]);

        $save_path=Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.'portal'.DIRECTORY_SEPARATOR.'['.$user_id.']'.$data['info']['user_nickname'].'.docx';
       // $tmp->saveAs('H:/rzjj/rzjj/public/html/word/'.$user_id.'.docx');//另存为
        $tmp->saveAs($save_path);//另存为
        header('location:'.$url.'/upload/portal/['.$user_id.']'.$data['info']['user_nickname'].'.docx');
    }
    public function get_userinfo($user_id){
        $model=new UserModel();
        $log=new ActivityLogModel();
        $upload=new ActivityUploadModel();
        $user_organize=new UserOrganizeModel();
        $info=$model->where('id',$user_id)->find();
        $community=Db::name('community')->where('id',$info['community'])->value('name');
        $organize=$user_organize->with('organize')->where(['user_id'=>$info['id'],'status'=>1])->select();
        $total_duration =Db::name('user_rank')->where('user_id',$user_id)->value('duration');
        $qians_log=$log->with('activity')->where('user_id',$user_id)->select();
        $zhuom_log=$upload->where('user_id',$user_id)->select();
        unset($info['user_pass']);
        $data=['info'=>$info,'sex'=>['保密','男','女'],'qians_log'=>$qians_log,'zhuom_log'=>$zhuom_log,'total_duration'=>$total_duration,'community'=>$community,'organize'=>$organize];
        return $data;
    }
    public function test(){
        $path=Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.'qrcode'.DIRECTORY_SEPARATOR;
        echo $path;
    }

}